class UsersController < ApplicationController
  
  layout 'layout'
  
  before_filter :authorize, :except => [:new, :create]
  
  def index
    @users = User.paginate :page => params[:page]
  end

  def show
    unless params[:id].blank?
      @user = User.find params[:id]
    else
      @user = current_user
    end  
  end

  def new
    @user = User.new
  end

  def edit
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      session[:user_id] = @user.id
      flash[:notice] = "User #{@user.name} created"
      redirect_to current_user_url
    else
      render :action => "new"
    end
  end

  def update
    @user = current_user
    if @user.update_attributes(params[:user])
       flash[:notice] = "#{@user.name} update complete!"
       redirect_to :action => "show"
     else
       flash[:notice] = "#{@user.name} update faile!"
       render :action => "edit"
    end
    
  end
  
  def destroy
    user = User.find(params[:id])
    user.destroy
    redirect_to :action => "index"
  end
end
